<?php
namespace application\admin\panels;

use application\utilities\Utility;
use application\models\ContentType;
class home extends panel {
	public $name = 'Welcome to Concerto';
	
	public function sidebar() {
		$stubs = DB()->getCount("SELECT * FROM `cn_content` WHERE `content_status`='stub'");
		$stubTypes = DB()->getResults("SELECT `ct_plural`, `ct_id`, COUNT(`content_id`) AS `count` FROM `cn_content` INNER JOIN `cn_content_types` ON `content_type`=`ct_id` WHERE `content_status`='stub' GROUP BY (`content_type`)");
		if($stubs) {
			$stubTypeText = array();
			foreach($stubTypes as $stub) {
				$stubTypeText[] = '<a href="javascript:concerto.admin.loadPanel(\'manage\', {type:'.$stub->ct_id.',status:\'stub\'});">' .$stub->count . ' '. $stub->ct_plural.'</a>';
			}
		
		}
		else {
			$stubs = '0';
		}
		
		$pendings = DB()->getCount("SELECT * FROM `cn_content` WHERE `content_status`='pending'");
		$pendingTypes = DB()->getResults("SELECT `ct_plural`, `ct_id`, COUNT(`content_id`) AS `count` FROM `cn_content` INNER JOIN `cn_content_types` ON `content_type`=`ct_id` WHERE `content_status`='pending' GROUP BY (`content_type`)");
		if($pendings) {
			$pendingTypeText = array();
			foreach($pendingTypes as $pending) {
				$pendingTypeText[] = '<a href="javascript:concerto.admin.loadPanel(\'manage\', {type:'.$pending->ct_id.',status:\'pending\'});">' .$pending->count . ' '. $pending->ct_plural.'</a>';
			}
		
		}
		else {
			$pendings = '0';
		}
		
		$totalCount = DB()->getCount("SELECT `content_id` FROM `cn_content`");
		$totalUsers = DB()->getCount("SELECT `user_id` FROM `cn_users`");
		?>
		<div class="inner">
			<h3>Site Statistics</h3>
			<ul class="sidebar-list">
				<li>
					You have <b><?=$pendings?></b> pending contents. 
					<?php if($pendings) : ?>
					<ul>
					<?php foreach($pendingTypeText as $pending) : ?>
						<li><?=$pending?></li>
					<?php endforeach;?>
					</ul>
					<?php endif;?>
				</li>
				<li>
					You have <b><?=$stubs?></b> stubs to fill out.
					<?php if($stubs) : ?>
					<ul>
					<?php foreach($stubTypeText as $stub) : ?>
						<li><?=$stub?></li>
					<?php endforeach;?>
					</ul>
					<?php endif;?>
				</li>
				<li>
					There are <b><?=$totalCount?></b> pieces of content on your site.
				</li>
				<li>
					There are <b><?=$totalUsers?></b> users on your site.
				</li>
				<?php if(getOption('user_admin_approval')) : ?>
				<li>
					<?php $pendingUsers = DB()->getCount("SELECT `user_id` FROM `cn_users` WHERE `user_level` < '0'");?>
					There are <b><?=$pendingUsers?></b> accounts awaiting approval.
				</li>
				<?php endif;?>
			</ul>
		</div>
		<?php // Get latest version from torchd site
		$latestVersion = file_get_contents('http://concerto.torchdm.com/download/latest/version.txt');
		$currentVersion = getOption('current_version');
		if($latestVersion > $currentVersion) :?>
		<div class="inner">
			<h3>There is a new version of Concerto available! (Version <?=$latestVersion?>)</h3>
			Click <a href="javascript:concerto.admin.loadPanel('update');">here</a> to update.
		</div>
		
		<?php endif;
	}
	
	public function newContentType() {
		$contentType = new ContentType;
		$contentType->create(array('title'=>$_REQUEST['singular'], 'plural'=>$_REQUEST['plural']));
		$messages = $contentType->add();
		$this->mergeMessages($messages);
		
		echo json_encode(array('messages'=>$this->messages, 'id'=>$contentType->getId()));
	}
	
	public function handleData() {
		global $concerto;

		if($_SESSION['fresh_install']) {
			$format = $_SESSION['site_format'];
			if($format) {
				require CN_APPLICATION_PATH . 'formats/'.$format.'/install.php';
				setOption('site_format', $format);
				$this->addMessage('success', ucwords($format).' format installed.');
			}
			unset($_SESSION['fresh_install']);
		}
		if($_REQUEST['clear_errors']) {
			DB()->query("DELETE FROM `cn_pageviews` WHERE `pv_type`='error'");
			$this->successes[] = 'Errors cleared successfully.';
		}
		
		elseif($_REQUEST['send_feedback']) {
			/** For now, just send us feedback via e-mail */
			$from = $concerto->User->email;
			$message = $_REQUEST['feedback'];
			mail('jason@limitdesigns.com', 'Concerto Feedback', $message, 'From: '.$from);
			$messages['success'] = "Thanks for sending us some feedback. We'll get back to you soon.";
			$this->mergeMessages($messages);
		}
		
		// Check for updates
		/**if($contents = self::checkForUpdates()) {
			$this->alerts[] = 'A new version of Concerto ('.$contents->version.') is available. Click <a href="'.CN_URL.'admin/?display=update">here</a> to install';
			$this->alerts[] = '<b>Version Notes: </b><br />'.$contents->notes;
		
		}*/
		
		
	}
	
	/**protected static function checkForUpdates() {
		global $concerto;
		$contents = file_get_contents('http://www.limitdesigns.com/concerto/latest_version/version.php');
		$contents = json_decode($contents);
		$version = $contents->version;
		$currentVersion = $concerto->getOption('current_version');
		if($version > $currentVersion) return $contents;
		else return FALSE;
	}*/
	
	public function title() {
		return CN_SITE_NAME . ' Administration';
	}
	
	
	public function display() {
		global $concerto;
		
		Modulator()->runFunction('admin_home');?>
		<div class="box">
			<h3>Welcome back.</h3>
			<div class="box-content">
				<div class="two-column-left">
				<h4>Latest Updates (Version 2.1.0.1)</h4>
				<ul class="box-list">
				<li>
					<h5>New User Configuration Options</h5>
					You now have more control over how users sign up for your site, as well as how they are identified on your site. Check out the user configuration panel for more.
				</li>
				<li class="geek-mode">
					<h5>Quantifiers and Qualifiers for Content/Content and User/Content Relations</h5>
					For linear/triangular content relations, as well as user-content relations, there are now quantifiers and qualifiers to better describe the relation. E.g, you could have a triangular content relation with three books, a movie, and two music CDs, or have a user purchase 100 gift baskets, without having to add 100 relation entries.
				</li>
				</ul>
				</div>
				<div class="two-column-right">
					<h4>Make the most out of Concerto</h4>
					<p>Check out the <a href="http://concerto.torchdm.com/" target="_blank">official Concerto website</a> for information on how to create templates, make complex data structures, create custom data types, make your own modules, and more!</p>
				</div>
			</div>
		</div>
		
		
		<?php
	}
}
?>